package com.gamiao.gwt.demo.useSpring.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserListPanel extends VerticalPanel {

	private Label message;

	private Button refresh;

	private Button insert;

	private CheckBox isShowDialog;

	private FlexTable list;

	private DockPanel listPanel;

	private UserServiceAsync userService;

	private UserEditorDialogWidget userEditorDialogWidget;

	private UserEditorPanelWidget userEditorPanelWidget;

	private UserSaveSucceedEventListener userSaveSucceedEventListener;

	public UserListPanel() {
		listPanel = new DockPanel();
		listPanel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
		listPanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		isShowDialog = new CheckBox("is show Dialog");
		isShowDialog.addClickListener(new IsShowDialogCheckBoxClickListener());
		list = new FlexTable();
		userEditorDialogWidget = new UserEditorDialogWidget("0", "", "");
		userEditorPanelWidget = new UserEditorPanelWidget("0", "", "");
		if (userSaveSucceedEventListener == null) {
			userSaveSucceedEventListener = new UserSaveSucceedEventListener();
			userEditorDialogWidget
					.addUserSaveSucceedEventListenerCollection(userSaveSucceedEventListener);
			userEditorPanelWidget
					.addUserSaveSucceedEventListenerCollection(userSaveSucceedEventListener);
		}
		userEditorDialogWidget.hide();

		list.setTitle("User List");
		list.setStyleName("crystal");
		list.setCellPadding(2);
		list.setCellSpacing(2);
		list.setHTML(0, 0, "User ID");
		list.setHTML(0, 1, "Frist Name");
		list.setHTML(0, 2, "Last Name");
		list.setHTML(0, 3, "Edit");
		list.setHTML(0, 4, "Delete");
		list.getRowFormatter().setStyleName(0, "head");

		message = new Label();
		listPanel.add(message, DockPanel.NORTH);
		initalInsertButton();
		initalFreshButton();
		listPanel.add(refresh, DockPanel.NORTH);
		listPanel.add(list, DockPanel.CENTER);
		listPanel.add(insert, DockPanel.SOUTH);
		listPanel.add(isShowDialog, DockPanel.SOUTH);
		refreshListTable();

		this.add(listPanel);
		this.add(userEditorPanelWidget);

	}

	private class IsShowDialogCheckBoxClickListener implements ClickListener {
		public void onClick(Widget sender) {
			if (isShowDialog.isChecked()) {
				userEditorPanelWidget.setVisible(false);
			} else {
				userEditorPanelWidget.setVisible(true);
			}
		}
	}

	private void initalFreshButton() {
		refresh = new Button("refresh");
		refresh.addClickListener(new RefreshButtonClickListener());
	}

	public void refreshListTable() {

		getUserClientService();
		userService.findAll(new FreshListCallback());
	}

	private class FreshListCallback implements AsyncCallback {
		public void onFailure(Throwable error) {
			message.setText("List user Failed!");
		}

		public void onSuccess(Object result) {
			PersonClient[] personClients = (PersonClient[]) result;
			for (int rowNumber = 1; rowNumber <= personClients.length; rowNumber++) {
				Button editButton = new Button("Edit");
				Button deleteButton = new Button("Delete");
				editButton.addClickListener(new EditButtonClickListener(
						rowNumber));
				deleteButton.addClickListener(new DeleteButtonClickListener(
						rowNumber));

				list.setText(rowNumber, 0, String
						.valueOf(personClients[rowNumber - 1].getId()));
				list.setText(rowNumber, 1, personClients[rowNumber - 1]
						.getFirstName());
				list.setText(rowNumber, 2, personClients[rowNumber - 1]
						.getLastName());
				list.setWidget(rowNumber, 3, editButton);
				list.setWidget(rowNumber, 4, deleteButton);

				list.getRowFormatter().setStyleName(rowNumber,
						rowNumber % 2 == 0 ? "even" : "odd");
			}
			int listMaxRowNumber = list.getRowCount();
			for (int rowNumber = personClients.length + 1; rowNumber < listMaxRowNumber; rowNumber++) {
				list.removeRow(rowNumber);
			}
			message.setText("List user Success!");
		}
	}

	private void initalInsertButton() {
		insert = new Button("Insert a new one.");
		insert.addClickListener(new InsertButtonClickListener());
	}

	private class DeleteUserCallback implements AsyncCallback {
		public void onFailure(Throwable error) {
			message.setText("Delete user Failed!");
		}

		public void onSuccess(Object result) {
			refreshListTable();
			message.setText("Delete user Success!");
		}
	}

	private class RefreshButtonClickListener implements ClickListener {
		public void onClick(Widget arg0) {
			refreshListTable();
		}
	}

	private class InsertButtonClickListener implements ClickListener {
		public void onClick(Widget arg0) {
			if (isShowDialog.isChecked()) {
				userEditorDialogWidget.setUserInfo("0", "", "");
				userEditorDialogWidget.center();
				userEditorDialogWidget.show();
			} else {
				userEditorPanelWidget.setUserInfo("0", "", "");
				userEditorPanelWidget.setVisible(true);
			}
		}
	}

	private class UserSaveSucceedEventListener implements ChangeListener {

		public void onChange(Widget arg0) {
			getUserClientService();
			userService.findAll(new FreshListCallback());
		}

	}

	private class DeleteButtonClickListener implements ClickListener {

		public int rowNumber;

		public DeleteButtonClickListener(int row) {
			rowNumber = row;
		}

		public void onClick(Widget sender) {
			String userID_inTable = list.getText(rowNumber, 0);
			getUserClientService();
			userService.remove(Integer.valueOf(userID_inTable).intValue(),
					new DeleteUserCallback());
		}
	}

	private class EditButtonClickListener implements ClickListener {

		public int rowNumber;

		public EditButtonClickListener(int row) {
			rowNumber = row;
		}

		public void onClick(Widget sender) {
			String userID_inTable = list.getText(rowNumber, 0);
			String firstName_inTable = list.getText(rowNumber, 1);
			String lastName_inTable = list.getText(rowNumber, 2);

			if (isShowDialog.isChecked()) {
				userEditorDialogWidget.setUserInfo(userID_inTable,
						firstName_inTable, lastName_inTable);
				userEditorDialogWidget.center();
				userEditorDialogWidget.show();
			} else {
				userEditorPanelWidget.setUserInfo(userID_inTable,
						firstName_inTable, lastName_inTable);
				userEditorPanelWidget.setVisible(true);
			}
		}
	}

	private void getUserClientService() {
		if (userService == null) {
			userService = UserService.Util.getInstance();
		}
	}
}
